﻿Imports System.Data.SqlClient

Public Class frmCatalogoAutobusesDetalle
    Dim Autobus As New clsCatalogoAutobuses
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal
    Private mstrCondicion As String
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property

    Private Sub frmCatalogoAutobusesDetalle_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró al catálogo detallado de Autobuses")
        Usuario.Ubicacion("Catálogo Autobuses")
    End Sub
    Private Sub frmCatalogoAutobusesDetalle_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        txtNoInterno.Text = AutobusSelect
        
        CargaRutas()
        CargaOptoControl()
        CargaAutobuses()

        Bitacora.RegistraActividad("Ingresó al catálogo detallado de Autobuses")
        Usuario.Ubicacion("Catálogo detallado de Autobuses")
    End Sub
    Sub CargaRutas()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        dgBuscarRutas.Rows.Clear()

        Dim Filtro As String
        Filtro = UCase(txtBuscarRuta.Text)
        Filtro = "%" & Filtro.Replace(" ", "%") & "%"

        Dim cnConn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "Select * from Rutas WHERE UPPER(Nombre) LIKE '" & Filtro & "' " & mstrCondicion

            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql


            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                dgBuscarRutas.Rows.Add(rdBuscar("Nombre").ToString.Trim)
                dgBuscarRutas.CurrentCell = dgBuscarRutas.Rows(0).Cells(0)
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub
    Sub CargaOptoControl()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        dgBuscarOptocontrol.Rows.Clear()

        Dim Filtro As String
        Filtro = UCase(txtBuscarOptocontrol.Text)
        Filtro = "%" & Filtro.Replace(" ", "%") & "%"

        Dim cnConn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = OptocontrolConnection
            strSql = "Select Cod_Vehiculo, Numero_Esclavo from Vehiculo WHERE UPPER(COD_Vehiculo) LIKE '" & Filtro & "' " & mstrCondicion & " OR UPPER(Numero_Esclavo) LIKE '" & Filtro & "' " & mstrCondicion
            'strSql = "Select Cod_Vehiculo, Numero_Esclavo from Vehiculo"
            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql


            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                dgBuscarOptocontrol.Rows.Add(rdBuscar("Cod_Vehiculo").ToString.Trim, rdBuscar("Numero_Esclavo").ToString.Trim)
                dgBuscarOptocontrol.CurrentCell = dgBuscarOptocontrol.Rows(0).Cells(1)
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub
    Sub CargaAutobuses()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        dgBuscarAutobus.Rows.Clear()

        Dim Filtro As String
        Filtro = UCase(txtBuscarAutobus.Text)
        Filtro = "%" & Filtro.Replace(" ", "%") & "%"

        Dim cnConn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "Select cCodigoC01,NoEconomico from Autobuses WHERE UPPER(cCodigoC01) LIKE '" & Filtro & "' " & mstrCondicion & " OR UPPER(NoEconomico) LIKE '" & Filtro & "' " & mstrCondicion

            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql


            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                dgBuscarAutobus.Rows.Add(rdBuscar("cCodigoC01").ToString.Trim, rdBuscar("NoEconomico").ToString.Trim)
                dgBuscarAutobus.CurrentCell = dgBuscarAutobus.Rows(0).Cells(0)
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub
    Sub Actualizar()
        Dim strNombre As String = ""
        If txtNoInterno.Text <> "" Then
            strNombre = Me.txtNoInterno.Text
            Autobus.cCodigoC01 = strNombre
        Else
            strNombre = ""
            Autobus.cCodigoC01 = strNombre
        End If
        Autobus.Buscar()
        Scatter()
    End Sub
    Private Sub Scatter()
        txtNoEcocnomico.Text = Autobus.NoEconomico
        txtOptocontrol.Text = Autobus.Numero_Esclavo
        If Autobus.TipoKms = 0 Then
            lstTipoKms.SelectedItem = "No Definido"
        ElseIf Autobus.TipoKms = 1 Then
            lstTipoKms.SelectedItem = "Odometro"
        Else
            lstTipoKms.SelectedItem = "OptoControl"
        End If
        If Autobus.Estatus = 0 Then
            lstEstatus.SelectedItem = "Inactivo"
        Else
            lstEstatus.SelectedItem = "Activo"
        End If
        txtRuta.Text = BuscarStrDatoCitra("Rutas", "Nombre", "IdRuta=" & Autobus.IdRuta)
        txtPlacas.Text = Autobus.Placas
        txtMarca.Text = Autobus.Marca
        txtModelo.Text = Autobus.Modelo
        txtSerie.Text = Autobus.Serie
        txtMotor.Text = Autobus.Motor
        txtRFID.Text = Autobus.Texto04


    End Sub
    Public Sub Gatter()
        Autobus.IdAutobus = BuscarIntDatoCitra("Autobuses", "IdAutobus", "cCodigoC01='" & txtNoInterno.Text.Trim & "'")
        Autobus.NoEconomico = txtNoEcocnomico.Text
        Autobus.IdRuta = BuscarIntDatoCitra("Rutas", "IdRuta", "Nombre='" & txtRuta.Text.Trim & "'")
        If lstTipoKms.Text = "No Indicado" Then
            Autobus.TipoKms = 0
        ElseIf lstTipoKms.Text = "Odometro" Then
            Autobus.TipoKms = 1
        ElseIf lstTipoKms.Text = "OptoControl" Then
            Autobus.TipoKms = 2
        End If
        Autobus.Cod_Vehiculo = BuscarStrDatoOptoControl("Vehiculo", "Cod_Vehiculo", "Numero_Esclavo=" & txtOptocontrol.Text)
        Autobus.Numero_Esclavo = txtOptocontrol.Text
        Autobus.cIdClien01 = Autobus.IdAutobus
        Autobus.cCodigoC01 = txtNoInterno.Text.Trim
        Autobus.cRazonSo01 = txtNoEcocnomico.Text
        Autobus.Placas = txtPlacas.Text
        Autobus.Marca = txtMarca.Text
        Autobus.Modelo = txtModelo.Text
        Autobus.Serie = txtSerie.Text
        Autobus.Motor = txtMotor.Text
        If lstEstatus.Text = "Inactivo" Then
            Autobus.Estatus = 0
        ElseIf lstEstatus.Text = "Activo" Then
            Autobus.Estatus = 1
        End If
        Autobus.Importe01 = 0
        Autobus.Importe02 = 0
        Autobus.Importe03 = 0
        Autobus.Importe04 = 0
        Autobus.Importe05 = BuscarIntDatoCitra("MGW10002", "cIdValor02", "cIdClien01=" & Autobus.IdAutobus)
        Autobus.Texto01 = ""
        Autobus.Texto02 = BuscarStrDatoOptoControl("SELECT ID_VEHICULO FROM VEHICULO WHERE NUMERO_ESCLAVO=" & txtOptocontrol.Text)
        Autobus.Texto03 = ""
        Autobus.Texto04 = txtRFID.Text
        Autobus.Texto05 = ""
    End Sub
    Private Sub txtBuscarOptocontrol_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtBuscarOptocontrol.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            SeleccionarOptocontrol()
        ElseIf e.KeyChar = Chr(Keys.Escape) Then
            panelOptocontrol.Visible = False
            txtOptocontrol.SelectAll()
            txtOptocontrol.Focus()
        End If

    End Sub

    Private Sub txtBuscarOptocontrol_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBuscarOptocontrol.TextChanged
        CargaOptocontrol()
    End Sub

    Private Sub txtBuscarRuta_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtBuscarRuta.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            SeleccionarRuta()
        ElseIf e.KeyChar = Chr(Keys.Escape) Then
            panelRutas.Visible = False
            txtRuta.SelectAll()
            txtRuta.Focus()
        End If
    End Sub
    Private Sub txtBuscarAutobus_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtBuscarAutobus.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            SeleccionarAutobus()
        ElseIf e.KeyChar = Chr(Keys.Escape) Then
            PanelAutobuses.Visible = False
            txtNoInterno.SelectAll()
            txtNoInterno.Focus()
        End If
    End Sub

    Private Sub txtBuscarAutobus_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBuscarAutobus.TextChanged
        CargaAutobuses()

    End Sub
    Private Sub dgBuscarAutobus_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgBuscarAutobus.DoubleClick
        SeleccionarAutobus()
    End Sub

    Private Sub dgBuscarAutobus_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgBuscarAutobus.KeyPress
        SeleccionarAutobus()
    End Sub
    Private Sub dgBuscarOptocontrol_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgBuscarOptocontrol.DoubleClick
        SeleccionarOptocontrol()
    End Sub

    Private Sub dgBuscarOptocontrol_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgBuscarOptocontrol.KeyPress
        SeleccionarOptocontrol()
    End Sub
    Private Sub dgBuscarRutas_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgBuscarRutas.DoubleClick
        SeleccionarRuta()
    End Sub

    Private Sub dgBuscarRutas_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgBuscarRutas.KeyPress
        SeleccionarRuta()
    End Sub
    Sub SeleccionarRuta()
        Dim valorCell As Integer
        Dim valorRow As DataGridViewRow

        valorCell = dgBuscarRutas.CurrentCell.ColumnIndex
        valorRow = dgBuscarRutas.CurrentRow

        Dim RutaMostrar As String = dgBuscarRutas.Rows(valorRow.Index).Cells(0).Value.ToString.Trim
        txtRuta.Text = RutaMostrar

        panelRutas.Visible = False
        txtRuta.SelectAll()
        txtRuta.Focus()
    End Sub
    Sub SeleccionarAutobus()
        Dim valorCell As Integer
        Dim valorRow As DataGridViewRow

        valorCell = dgBuscarAutobus.CurrentCell.ColumnIndex
        valorRow = dgBuscarAutobus.CurrentRow

        Dim AutobusMostrar As String = dgBuscarAutobus.Rows(valorRow.Index).Cells(0).Value.ToString.Trim
        txtNoInterno.Text = AutobusMostrar

        PanelAutobuses.Visible = False
        txtNoInterno.SelectAll()
        txtNoEcocnomico.Focus()
    End Sub
    Sub SeleccionarOptocontrol()
        Dim valorCell As Integer
        Dim valorRow As DataGridViewRow

        valorCell = dgBuscarOptocontrol.CurrentCell.ColumnIndex
        valorRow = dgBuscarOptocontrol.CurrentRow

        Dim OptocontrolSelect As String = dgBuscarOptocontrol.Rows(valorRow.Index).Cells(1).Value.ToString.Trim
        txtOptocontrol.Text = OptocontrolSelect

        panelOptocontrol.Visible = False
        txtOptocontrol.SelectAll()
        txtOptocontrol.Focus()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        PanelAutobuses.Visible = True
        txtBuscarAutobus.Clear()
        txtBuscarAutobus.Focus()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        panelOptocontrol.Visible = True
        txtBuscarOptocontrol.Clear()
        txtBuscarOptocontrol.Focus()
    End Sub

    Private Sub txtNoInterno_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtNoInterno.TextChanged
        Actualizar()

    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Close()
    End Sub

    Private Sub RegresarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RegresarToolStripMenuItem.Click
        Close()
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        panelRutas.Visible = True
        txtBuscarRuta.Clear()
        txtBuscarRuta.Focus()
    End Sub

    Private Sub txtBuscarRuta_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBuscarRuta.TextChanged
        CargaRutas()

    End Sub

    Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
        Guardar()
    End Sub
    Sub Guardar()
        Gatter()
        Autobus.ActualizarConOpto()
        If Autobus.Estatus = 1 Then
            Autobus.ActualizarAdminPAQ()
        End If
    End Sub

    Private Sub GuardarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles GuardarToolStripMenuItem.Click
        Guardar()

    End Sub
End Class